package com.example.gkcircle.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVUser;
import com.example.gkcircle.BaseFragment;
import com.example.gkcircle.R;
import com.example.gkcircle.activity.LoginActivity;
import com.example.gkcircle.activity.PhotoViewAcitivty;
import com.example.gkcircle.activity.RegisterActivity;
import com.example.gkcircle.activity.ResetPasswordActivity;
import com.example.gkcircle.library.object.GKUser;
import com.example.gkcircle.utils.ActivityUtil;
import com.example.gkcircle.utils.AsynImageLoader;
import com.example.gkcircle.utils.PicUtil;
import com.example.gkcircle.utils.UniversalImageLoader;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 我的Fragment
 * @author ZhouHua
 * created at 15/12/21 上午12:55
*/
public class PersonalFragment extends BaseFragment implements View.OnClickListener
{

	private CircleImageView mUserIconIv;
	private ImageView mUserSexIv;
	private TextView mUsernameText;
	private TextView mUserSexText;
	private TextView mDepartmentText;
	private LinearLayout mInfoLayout;
	private LinearLayout mComposedLayout;
	private LinearLayout mQuetionLayout;
	private LinearLayout mChangePswLayout;
	private LinearLayout mSettingsLayout;
	private String  thumbnailUrl;

	@Override
	protected int setLayout() {
		return R.layout.fragment_personal;
	}

	@Override
	protected void initView(View view) {
		mUserIconIv=(CircleImageView)view.findViewById(R.id.personal_user_icon);
		mUserSexIv=(ImageView)view.findViewById(R.id.personal_sex_icon);
		mUsernameText=(TextView)view.findViewById(R.id.personal_username);
		mUserSexText=(TextView)view.findViewById(R.id.personal_sex);
		mDepartmentText=(TextView)view.findViewById(R.id.personal_department);

		mInfoLayout=(LinearLayout)view.findViewById(R.id.personal_info_layout);
		mComposedLayout=(LinearLayout)view.findViewById(R.id.personal_composed_layout);
		mQuetionLayout=(LinearLayout)view.findViewById(R.id.personal_quetion_layout);
		mChangePswLayout=(LinearLayout)view.findViewById(R.id.personal_change_psw_layout);
		mSettingsLayout=(LinearLayout)view.findViewById(R.id.personal_settings_layout);

	}

	@Override
	protected void initListener() {
		mUserIconIv.setOnClickListener(this);
		mInfoLayout.setOnClickListener(this);
		mComposedLayout.setOnClickListener(this);
		mQuetionLayout.setOnClickListener(this);
		mChangePswLayout.setOnClickListener(this);
		mSettingsLayout.setOnClickListener(this);

	}

	@Override
	protected void setData() {
		AVUser user=AVUser.getCurrentUser();
		if(user==null){
			ActivityUtil.startActivity(getActivity(), LoginActivity.class);
			return;
		}

		mUserSexIv.setBackground((Boolean) user.get("sex") ? getResources().getDrawable(R.mipmap.personal_male) : getResources().getDrawable(R.mipmap.personal_female));
		mUsernameText.setText(user.getUsername().toString());
		mUserSexText.setText((Boolean) user.get("sex") ? "男" : "女");
		mDepartmentText.setText(user.get("department") == null ? "" : user.get("department").toString());

		if(null!=user.getAVFile("avatar")){
			thumbnailUrl=user.getAVFile("avatar").getThumbnailUrl(true, PicUtil.dip2px(getActivity(), 70),PicUtil.dip2px(getActivity(),70));
			UniversalImageLoader.getIntance().displayImage(thumbnailUrl,mUserIconIv,UniversalImageLoader.getAvatarOptions(),UniversalImageLoader.getAnimateFirstDisplayListener());
			return;
		}
		mUserIconIv.setImageResource(R.mipmap.personal_default_user_icon);
	}



	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.personal_user_icon:
				if(thumbnailUrl!=null){
					Intent intent=new Intent(getActivity(),PhotoViewAcitivty.class);
					Bundle bundle=new Bundle();
					bundle.putString("imageUrl", AVUser.getCurrentUser().getAVFile("avatar").getUrl());
					bundle.putString("thumbnailUrl",thumbnailUrl);
					intent.putExtras(bundle);
					startActivity(intent);
				}
				break;
			case R.id.personal_info_layout:
				break;
			case R.id.personal_composed_layout:
				break;
			case R.id.personal_quetion_layout:
				break;
			case R.id.personal_change_psw_layout:
				getActivity().overridePendingTransition(0, R.anim.zoom_in);
				Intent intent =new Intent(getActivity(),ResetPasswordActivity.class);
				Bundle bundle=new Bundle();
				bundle.putInt("flag", 0);
				intent.putExtras(bundle);
				getActivity().startActivity(intent);
				break;
			case R.id.personal_settings_layout:
				AVUser.logOut();
				break;
		}
	}
}
